
"""
change view for InfoList div.
  new view name in cgi require Viewing.
  return the page after modification.
"""

import chatLogic
import getChatData
from whiff.middleware import misc
from whiff import resolver
from whiff import whiffenv

class ChangeView(misc.utility):
    def __init__(self, page):
        self.page = page
    def __call__(self, env, start_response):
        env = resolver.process_cgi(env)
        cgi_dict = env[whiffenv.CGI_DICTIONARY]
        viewings = cgi_dict.get("Viewing")
        if not viewings:
            raise ValueError, "changeView requires 'Viewing' cgi require"
        viewing = viewings[0]
        #connection = chatLogic.test_gadfly_connection(whiffenv.getId(env, "DbPrefix"))
        connection = chatLogic.getConnection(env)
        nick = chatLogic.getCookieValue(env)
        #pr "changeview setting view", (nick, viewing)
        connection.setView(nick, viewing)
        connection.commit()
        app = getChatData.getChatData(self.page)
        return app(env, start_response)
    
__middleware__ = ChangeView
